<?php

if (!defined('APP')) {
    die('error');
}

class Pay extends PayBase
{

    //构造函数
    public function __construct()
    {
        parent::__construct();
        $this->getDb();
    }

    //通过用户账户来支付
    public function userZhangHuPay()
    {
        $s = 'fail';
        $body = "洗衣付款";
        $yue = $this->db->getnum('yue');
        $ri = $this->db->getnum('ri');
        $userId = $this->db->getnum('userId');
        $xingqi = $this->db->getval('xingqi');
        $count = $this->db->getnum('count');
        $yuYueShiDuan = $this->db->getval('yuYueShiDuan');
        $content = $this->db->getval('content');
        $openid = $this->db->getval('openid');
        $danhao = $this->db->getval('danhao');
        $yuYueLeiXing = $this->db->getval('tps');
        $name = $this->db->getval('name');
        $phone = $this->db->getval('phone');
        $address = $this->db->getval('address');
        $fee = $this->db->getnum('fee');
        $liuYan = $this->db->getval('liuYan');

        //查询符合条件的用户账户数额
        $query="select fee from " . QZ . "user where id={$userId} and fee > {$fee} ";
        $userRow=$this->db->query_getrow($query) ;
        if($userRow){
            //智能分配空闲员工配送
            $qiShouId = 0; 
            $query = "select id ,shijian from " . QZ . "user where status=1 order by shijian asc limit 1";
            $qiShouRow = $this->db->query_getrow($query);
            if ($qiShouRow) {
                $qiShouId = $qiShouRow['id'];
                $query = "update " . QZ . "user set shijian=now() where id={$qiShouId} ";
                $this->db->exec($query);
            }
    
            //存储订单信息
            $query = "insert into " . QZ . "yuYue(
                        yue,ri,count,liuYan,userId,qiShouId,
                        fee,xingqi,openid,danhao,content,status,
                        yuYueShiDuan,name,phone,address,yuYueLeiXing
                    )values(
                        {$yue},{$ri},{$count},'{$liuYan}', {$userId},{$qiShouId},
                        {$fee},'{$xingqi}','{$openid}','{$danhao}','{$content}',1,
                        '{$yuYueShiDuan}','{$name}','{$phone}','{$address}' ,'{$yuYueLeiXing}'
                    )";
            $this->db->exec($query);

            //减去用户账户对应额度
            $query="update ".QZ."user set fee=fee-{$fee} where id={$userId}";
            $this->db->exec($query);

            $s='success';            
        }

        //返回结果
        echo $s;
    }

    //更新充值订单号
    public function updateChongZhiDanHao()
    {
        $danhao = $this->db->getval('danhao');
        $openid = $this->db->getval('openid');
        $status = $this->db->getnum('status');
        if ($status == 1) {
            //检测是否未更新
            $query = "select id,fee from " . QZ . "ChongZhiDingDan where  danhao='{$danhao}' and openid='{$openid}' and status=0 ";
            $chongZhiRow = $this->db->query_getrow($query);
            $fee = $chongZhiRow['fee'] * 1.2;
            //更新用户账户余额
            $query = "update " . QZ . "user set fee = fee + {$fee} where openid = '{$openid}' ";
            $this->db->exec($query);
        }
        $query = "update " . QZ . "ChongZhiDingDan set status={$status} where danhao='{$danhao}' and openid='{$openid}' ";
        $this->db->idu($query);
    }

    //更新预约订单
    public function updateYuYueDingDan()
    {
        $danhao = $this->db->getval('danhao');
        $openid = $this->db->getval('openid');
        //预约订单更新支付状态
        $query = "update " . QZ . "yuYue set status=1  where danhao='{$danhao}'
				&& openid='{$openid}' && status=0 ";
        $this->log($query);
        echo $this->db->u($query);
    }

    //设置过期
    public function setGuoQi()
    {
        //获取15天之前的时间
        $query = "select date_sub( now(),interval 15 day) as lastEnd ";
        $row = $this->db->query_getrow($query);
        $lastEnd = $row['lastEnd'];
        $query = "update  " . QZ . "yuYue set status=50 where shijian< '{$lastEnd}' and status=40 ";
        $this->db->idu($query);
    }

    //设置自己订单的过期
    public function setMeGuoQi()
    {
        $openid = $this->db->getval('openid');
        $query = "update  " . QZ . "yuYue set status=50 where shijian <  date_sub( now(),interval 15 day)
				and status=40 and openid='{$openid}' ";
        $this->db->idu($query);

    }
}
